<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Log extends Controller
{
    private $_model;
    private $title_field = [

    ];
    public $pagesize = 15;

    public function _initialize()
    {
        $this->_model = new \app\admin\model\Log();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {

        return $this->fetch('index');
    }


    public function lists()
    {

        $starttime = trim(input("request.starttime"));//auth
        $endtime = trim(input("request.endtime"));//auth


        if (!empty($starttime) && !empty($endtime)) {
            $where['time'] = array('between', $starttime . "," . $endtime);
        }


        $lists = $this->_model->lists($where, $this->pagesize);
        if (!empty($lists['data'])) {

            $userList = array();

            $UserModel = new \app\admin\model\User();
            foreach ($lists['data'] as $key => $val) {
                $lists['data'][$key]['duserInfo'] = array();
                if ($val['user']) {
                    if ($userList[$val['user']]) {
                        $lists['data'][$key]['duserInfo'] = $userList[$val['user']];
                    } else {
                        $tempUserInfo = $UserModel->getID($val['user']);
                        if ($tempUserInfo) {
                            $userList[$tempUserInfo['id']] = $tempUserInfo;
                            $lists['data'][$key]['duserInfo'] = $tempUserInfo;
                        }
                    }
                }


            }
        }


        $this->assign('lists', $lists);


        //查询用户部门
        $depModel = new \app\admin\model\Dep();

        $tmpUserDepList = $depModel->getAll();//查询一级部门

        $userDepList = array();
        if ($tmpUserDepList) {
            foreach ($tmpUserDepList as $key => $val) {
                $userDepList[$val['id']] = $val;
            }
        }


        $this->assign('userDepList', $userDepList);
        $this->assign('request', input("request."));
        return view('lists');

    }


    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }

//         $oldGroup = $this->_model->getDataForWhere(array(
//             'id'=>$id
//         ));

        $res = $this->_model->delData(array('id' => $id));

        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }


    public function addData()
    {
        $time = time();
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $depname = trim(input("request.depname"));//auth
            $oldGroup = $this->_model->getDataForWhere(array(
                'depname' => $depname,
                'level' => 0,
            ));
            if (!empty($oldGroup)) {
                $return = ['err' => 3, 'msg' => '该部门已经存在，请不要重复添加'];
                return json($return);
            }

            $insertData = array(
                'depname' => $depname,

                'ctime' => $time,
            );
            $res = $this->_model->add($insertData);

            if ($res) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        //设置全局信息
        $title = "添加部门";
        $this->assign('title', $title);

        return view('addData');

    }


    public function deal()
    {

        $id = trim(input("request.id"));//auth

        //查询报修信息
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $this->assign('info', $info);


        //查询资产信息
        $zcModel = new \app\admin\model\Devlist();
        $zcInfo = $zcModel->getDataForWhere(array('id' => $info['devid']));

        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $op = trim(input("request.op"));//auth
            if ($op == 1) {
                $stats = trim(input("request.stats"));//auth
                $conts = trim(input("request.conts"));//auth
                $tmpConts = $conts;
                $currUser = $this->LoginUserInfo['UserInfo'];
                $date = date("Y-m-d H:i:s");

                $w = array(
                    'id' => $id
                );
                if ($stats == '0') {
                    $stat = 3;//不同意
                    $conts = "[" . $date . "][拒绝报修申请][" . $currUser['uname'] . "][" . $currUser['nickname'] . "] " . $conts;
                } else {
                    $stat = 1;//同意
                    $conts = "[" . $date . "][同意报修申请][" . $currUser['uname'] . "][" . $currUser['nickname'] . "] " . $conts;
                }


                $update = array(
                    'stat' => $stat,
//                     'conts'=>$tmpConts,
                    'log' => $info['log'] . "<br>" . $conts,
                    'etime' => $date,
                );
                $res = $this->_model->updateDataForWhere($update, $w);

                if ($res || ($res === 0)) {

                    //记录日志
                    $action = "";
                    if ($stats == '1') {
                        $action = "资产编号[" . $zcInfo['dveid'] . "]报修状态修改，同意维修";
                    } else {
                        $action = "资产编号[" . $zcInfo['dveid'] . "]报修状态修改，不同意维修";
                    }
                    $this->recordSystemLog([
                        'user' => $this->LoginUserInfo['UserInfo']['id'],
                        'time' => date("Y-m-d H:i:s"),
                        'action' => $action,
                        'type' => 3,
                    ]);
                    $return = ['err' => 0, 'msg' => '操作成功'];
                    return json($return);
                } else {
                    $return = ['err' => 3, 'msg' => '操作失败'];
                    return json($return);
                }
            } elseif ($op == 2) {

                $conts = trim(input("request.conts"));//auth
                $tmpConts = $conts;
                $currUser = $this->LoginUserInfo['UserInfo'];
                $date = date("Y-m-d H:i:s");

                $w = array(
                    'id' => $id
                );

                $conts = "[" . $date . "][受理中][" . $currUser['uname'] . "][" . $currUser['nickname'] . "] " . $conts;


                $update = array(
                    'stat' => 2,
//                     'conts'=>$tmpConts,
                    'log' => $info['log'] . "<br>" . $conts,
                    'etime' => $date,
                );
                $res = $this->_model->updateDataForWhere($update, $w);

                if ($res || ($res === 0)) {
                    //修改资产状态为报修状态
                    $devlistMode = new \app\admin\model\Devlist();
                    $devlistMode->updateDataForWhere(
                        [
                            'stat' => 0
                        ],
                        ['id' => $info['devid']]);


                    //记录日志
                    $action = "";

                    $action = "资产编号[" . $zcInfo['dveid'] . "]报修状态修改，维修中";

                    $this->recordSystemLog([
                        'user' => $this->LoginUserInfo['UserInfo']['id'],
                        'time' => date("Y-m-d H:i:s"),
                        'action' => $action,
                        'type' => 3,
                    ]);
                    $return = ['err' => 0, 'msg' => '操作成功'];
                    return json($return);
                } else {
                    $return = ['err' => 3, 'msg' => '操作失败'];
                    return json($return);
                }
            } elseif ($op == 3) {

                $conts = trim(input("request.conts"));//auth
                $tmpConts = $conts;
                $currUser = $this->LoginUserInfo['UserInfo'];
                $date = date("Y-m-d H:i:s");
                $stats = trim(input("request.stats"));//auth
                $w = array(
                    'id' => $id
                );
                $zcstat = 0;
                if ($stats == '1') {
                    $conts = "[" . $date . "][已修好，可使用][" . $currUser['uname'] . "][" . $currUser['nickname'] . "] " . $conts;
                } else {
                    $zcstat = 3;
                    $conts = "[" . $date . "][未修好，报废][" . $currUser['uname'] . "][" . $currUser['nickname'] . "] " . $conts;

                    //
                    $modelArchives = new \app\admin\model\Archives();
                    $idata = array(
                        'devid' => $id,
                        'user' => $zcInfo['uid'],
                        'dtime' => date("Y-m-d H:i:s"),
                        'info' => '',
                        'dep' => '0',
                        'first_dep' => '0',
                        'op_uid' => $currUser['id'],
                        'date' => date("Y-m-d"),
                        'ty' => 3,//类型 0入库，1领用 2归还 3报废
                    );

                    $modelArchives->add($idata);
                    //
                }


                $update = array(
                    'stat' => 4,
                    'log' => $info['log'] . "<br>" . $conts,
                    'etime' => $date,
                );
                $res = $this->_model->updateDataForWhere($update, $w);

                if ($res || ($res === 0)) {
                    //修改资产状态为报修状态
                    $devlistMode = new \app\admin\model\Devlist();
                    $devlistMode->updateDataForWhere(
                        [
                            'stat' => $zcstat
                        ],
                        ['id' => $info['devid']]);
                    //记录日志
                    $action = "";
                    if ($stats == '1') {
                        $action = "资产编号[" . $zcInfo['dveid'] . "]报修状态修改，已修好，可使用";
                    } else {
                        $action = "资产编号[" . $zcInfo['dveid'] . "]报修状态修改，未修好，报废";
                    }
                    $this->recordSystemLog([
                        'user' => $this->LoginUserInfo['UserInfo']['id'],
                        'time' => date("Y-m-d H:i:s"),
                        'action' => $action,
                        'type' => 3,
                    ]);
                    $return = ['err' => 0, 'msg' => '操作成功'];
                    return json($return);
                } else {
                    $return = ['err' => 3, 'msg' => '操作失败'];
                    return json($return);
                }
            }
        }


        //设置全局信息

        $this->assign('id', $id);
        if ($info['stat'] == '0')//刚刚提交
        {
            $title = "工单确认";
            $this->assign('title', $title);
            return view('deal');
        } else if ($info['stat'] == '1')//等待
        {
            $title = "工单维修受理";
            $this->assign('title', $title);
            return view('deal_sl');
        } else if ($info['stat'] == '2')//受理
        {
            $title = "工单完成";
            $this->assign('title', $title);
            return view('deal_wc');
        }

    }

    public function recordSystemLog($arr)
    {
        $model = new \app\admin\model\Log();
        $model->add($arr);
    }


}